package com.cskaoyan.demo6.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * @Description：
 * @Author：BLUEBLANK
 * @Date：2024/4/20 14:46
 */

@Configuration
@ComponentScan("com.cskaoyan.demo6")
@EnableAspectJAutoProxy
@EnableTransactionManagement
public class ApplicationConfiguration {
    @Bean
    public DataSource dataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        druidDataSource.setUrl("jdbc:mysql://localhost:3306/cskaoyan_market?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8");
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("123456");
        return druidDataSource;
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) {
        // 创建SqlSessionFactoryBean实例
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        // 设置数据源
        sqlSessionFactoryBean.setDataSource(dataSource);
        // 配置mybatis的分页插件
        PageInterceptor pageInterceptor = new PageInterceptor();
        // 设置分页插件的属性
        Properties properties = new Properties();
        // 表示分页查询时使用的数据库类型是MySQL
        properties.setProperty("helperDialect", "mysql");
        // 表示分页查询时是否进行优化，优化内存使用。
        // properties.setProperty("reasonable", "true");
        // // 表示分页查询时是否支持方法的参数
        // properties.setProperty("supportMethodsArguments", "true");
        // // 表示分页查询时，countSql表示的SQL语句用于计算总记录数
        // properties.setProperty("params", "count=countSql");
        // 配置分页插件
        pageInterceptor.setProperties(properties);
        sqlSessionFactoryBean.setPlugins(pageInterceptor);
        // 转换器配置
        sqlSessionFactoryBean.setTypeHandlersPackage("com.cskaoyan.demo6.typehandler");

        // 返回SqlSessionFactoryBean实例
        return sqlSessionFactoryBean;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.cskaoyan.demo6.mapper");
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        return mapperScannerConfigurer;
    }

    @Bean
    public DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}

